package com.xhwl.centiotdata.handler.alarm.impl;

import com.alibaba.fastjson.JSONObject;
import com.xhwl.centiotdata.handler.alarm.AlarmFactory;
import com.xhwl.centiotdata.handler.alarm.AlarmHandler;
import com.xhwl.centiotdata.utils.RedisUtils;
import com.xhwl.common.constant.cent.alarm.AlarmConstant;
import com.xhwl.common.dto.cent.alarm.iot.AccessInformation;
import com.xhwl.common.dto.cent.alarm.iot.ElectronicFenceAlarm;
import com.xhwl.common.dto.cent.alarm.iot.IotAlarmBody;
import com.xhwl.common.dto.cent.device.DeviceDTO;
import com.xhwl.common.pojo.cent.alarm.AlarmManagement;
import org.springframework.stereotype.Component;

import java.time.ZoneId;
import java.util.Date;
import java.util.Map;

/**
 * @Author Leon
 * @Description: 电子围栏告警
 *  TODO
 * @Date: 2022/3/22 17:29
 */
@Component
public class AlarmHuaWeiIotDAElectronicFenceAlarmHandler extends AlarmHandler {
    @Override
    public void afterPropertiesSet() throws Exception {
        AlarmFactory.register("ElectronicFenceAlarm", this);
    }

    @Override
    public void pushHuaWeiIotDAIotAlarmBody(IotAlarmBody t, DeviceDTO device) {
        ElectronicFenceAlarm electronicFenceAlarm = JSONObject.parseObject(JSONObject.toJSONString(t.getProperties()), ElectronicFenceAlarm.class);
        AlarmManagement alarmManagement = new AlarmManagement();
        alarmManagement.setAlarmTime(Date.from(t.getEvent_time().atZone(ZoneId.systemDefault()).toInstant()));
        alarmManagement.setDeviceId(device.getId());
        alarmManagement.setProjectId(device.getProjectId());
        Map<String, String> entries = redisTemplate.opsForHash().entries(RedisUtils.format(40));
        alarmManagement.setAlarmInventoryCode(entries.get(electronicFenceAlarm.getAlarmEvent()));
        sendMq(AlarmConstant.JINMAO_TOPIC,alarmManagement);
    }
}
